import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.postgresql.Driver;

/**
 * @作者 ： pwx5328113 @时间： 2023/4/13
 * @测试点: 打开快速负载均衡，同一集群，2个url连接
 **/
public class JDBC_QuikAutoBalance_Case0033 {
	public static void main(String[] args) throws Exception {
		System.out.println("----------start----------");
		System.out.println(Driver.getGSVersion());
		List<Connection> connections = new ArrayList<>();
		System.out.println("----------url1 进行10个建连----------");
		String url1 = "jdbc:postgresql://" + args[0] + "/" + args[1] + "?" + args[8];
		for (int i = 0; i < 10; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------url2 进行10个建连----------");
		String url2 = "jdbc:postgresql://" + args[4] + "/" + args[5] + "?" + args[9];
		for (int i = 0; i < 10; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url2, args[6], args[7]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		int sleep_time = 40;
		if (args.length > 10) {
			sleep_time = Integer.valueOf(args[10]).intValue();
		}
		System.out.println("----------sleep " + sleep_time + "----------");
		TimeUnit.SECONDS.sleep(sleep_time);

		System.out.println("----------url1 再次进行5个建连----------");
		for (int i = 0; i < 5; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url1, args[2], args[3]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------url2 再次进行7个建连----------");
		for (int i = 0; i < 7; i++) {
			try {
				Connection connection1 = DriverManager.getConnection(url2, args[6], args[7]);
				connections.add(connection1);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		System.out.println("----------sleep 15s----------");
		TimeUnit.SECONDS.sleep(10);

		System.out.println("----------connections close start----------");
		for (Connection connection : connections) {
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.out.println("----------connections close end----------");
	}
}
